package com.ddwl.message.controller;

import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.message.service.MessageService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.message.MessageSearchBo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.message.MessageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @author Fu Zhaohui
 * @date 2017/1/3
 */
@Api(tags="消息服务")
@Auth(product = {BaseEnum.ProductEnum.ZHENYU, BaseEnum.ProductEnum.CLOTHES, BaseEnum.ProductEnum.MALL})
@RestController
public class MessageController extends BaseController {

    @Autowired
    private MessageService messageService;


    @ApiOperation("消息列表")
    @PostMapping("/index")
    public RestVo<List<MessageVo>> index(@Valid @RequestBody PageBo<MessageSearchBo> pageBo) {
        MessageSearchBo param = Optional.ofNullable(pageBo.getParam()).orElse(new MessageSearchBo());
        param.setProduct(getProduct());
        param.setUserId(getUserId());
        return RestVo.ok(messageService.index(pageBo));
    }

    @DeleteMapping("del")
    @ApiOperation("删除消息")
    public RestVo delete(@RequestParam String id) {
        messageService.delete(id, getUserId());
        return RestVo.ok();
    }

    @GetMapping("/detail")
    @Auth(product = {BaseEnum.ProductEnum.ZHENYU, BaseEnum.ProductEnum.CLOTHES, BaseEnum.ProductEnum.MALL})
    @ApiOperation("消息详情")
    public RestVo<MessageVo> detail(@ApiParam(value = "消息ID", required = true) @RequestParam String id) {
        return RestVo.ok(messageService.detail(id));
    }

}
